import {Route} from "react-router-dom"
import {keySeparator, rootPath} from "init"
import React from "react"

/**
 * 根据菜单配置生成Route
 * @param menus
 * @param pKey
 * @param pPath
 */
const createRoute = (menus, pKey, pPath) => {

	return menus.reduce((p, m) => {
		const _path = pPath ? `${pPath}/${m.key}` : `${rootPath}/${m.key}`
		const _key = `${pKey || ""}${keySeparator}${m.key}`
		const _param = m.param ? `/:${m.param.key}` : ""

		return p.concat(
			m.children
				? createRoute(m.children, _key, _path)
				: <Route exact={true} path={`/${_path}${_param}`} component={m.component} key={_key}/>
		)
	}, [])
}

export {createRoute}